﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using TournamentDatabase.Objects;
using TournamentDatabase.Database;
using TournamentDatabase.Singletons;

namespace TournamentDatabase.DialogBoxes.Select
{
	/// <summary>
	/// Interaction logic for JudgeSelectDialogBox.xaml
	/// </summary>
	public partial class JudgeSelectDialogBox : Window
	{
		Judge m_selectedJudge;
		List<Judge> m_judgeList;
		string m_currentSearch;

		public List<Judge> JudgeList
		{
			get { return m_judgeList; }
			set { m_judgeList = value; }
		}

		public Judge SelectedJudge
		{
			get { return m_selectedJudge; }
			set { m_selectedJudge = value; }
		}

		public JudgeSelectDialogBox()
		{
			InitializeComponent();

			Search();

			this.DataContext = this;
		}

		private void selectButton_Click(object sender, RoutedEventArgs e)
		{
			if (m_selectedJudge == null)
				return;
			DialogResult = true;
		}

		private void OnSearch(string searchText)
		{
			m_currentSearch = searchText;
			Search();
		}

		private void Search()
		{
			if (m_currentSearch == null || m_currentSearch.Length == 0)
			{
				m_judgeList = DatabaseSingleton.GetDatabaseAccessor().QueryJudge("", OrderBy.None, "");
			}
			else
			{
				string where = "name LIKE '%" + m_currentSearch + "%'";
				m_judgeList = DatabaseSingleton.GetDatabaseAccessor().QueryJudge(where, OrderBy.None, "");
			}
		}
	}
}
